<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <h2>评论列表</h2>
    </div>
    <el-table :data="model" style="width: 100%" stripe border>
      <el-table-column type="index" label="#"/>
      <el-table-column prop="_id" label="ID" width="300px"/>
      <el-table-column prop="nickname" label="评论人"/>
      <el-table-column label="头像">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" alt="" style="width: 50px;height: 50px;">
        </template>
      </el-table-column>
      <el-table-column prop="content" label="内容"/>
      <el-table-column width="220px" label="操作">
        <!--通过 Scoped slot 可以获取到 row, column, $index 和 store（table 内部的状态管理）的数据-->
        <template slot-scope="scope">
          <el-button type="warning" size="mini" icon="el-icon-edit"
                     @click="$router.push(`/message/edit/${scope.row._id}`)">
            编辑
          </el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="remove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
  export default {
    name: "AdminUserList",
    data(){
      return{
        model:[]
      }
    },
    methods:{
      async getMessageData(){
        const res = await this.$http.get("/rest/message")
        console.log(res.data)
        this.model = res.data.list
      },
      async remove(row){
        const res = await this.$confirm(`是否要删除留言`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err=>err)
        if(res === 'confirm'){
          await this.$http.delete(`/rest/message/${row._id}`)
          this.$message({
            message: `成功删除留言`,
            type: 'success'
          });
          this.getCommentData()
        }else {
          this.$message({
            message: `取消删除操作`,
            type: 'info'
          });
        }
      }
    },
    created() {
      this.getMessageData()
    },

  }
</script>

<style lang="scss" scoped>
</style>
